#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5, maxl = 20;
int n, m, ap[maxn], a[maxn], c[maxn], k[maxn], d[maxl][maxn], X[4];
int ene[maxn], dp[maxl][maxn], mem[maxn];
int getwinner(int k, int n) {
    for (int i = 1; i <= n; i++) {
        dp[0][i] = i;
    }
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= (1 << (k - i)); j++) {
            if (d[i][j]) {
                if (ene[dp[i - 1][j * 2]] >= i) {
                    dp[i][j] = dp[i - 1][j * 2];
                } else {
                    dp[i][j] = dp[i - 1][j * 2 - 1];
                }
            } else {
                if (ene[dp[i - 1][j * 2 - 1]] >= i) {
                    dp[i][j] = dp[i - 1][j * 2 - 1];
                } else {
                    dp[i][j] = dp[i - 1][j * 2];
                }
            }
        }
    }
    return dp[k][1];
}
void solve() {
    scanf("%d %d %d %d", &X[0], &X[1], &X[2], &X[3]);
    for (int i = 1; i <= n; i++) {
        a[i] = ap[i] ^ X[i % 4];
        mem[i] = -1;
    }
    bool ispw = 1;
    int mxk = 0;
    for (int i = 1; i <= m; i++) {
        k[i] = 0;
        while ((1LL << k[i]) < c[i]) {
            k[i]++;
        }
        mxk = max(mxk, k[i]);
        ispw &= (1 << k[i]) == c[i];
    }
    if (ispw) {
        for (int i = 1; i <= n; i++) {
            ene[i] = a[i];
        }
        for (int i = n + 1; i <= (1 << mxk); i++) {
            ene[i] = 0;
        }
        getwinner(mxk, 1 << mxk);
        ll ans = 0;
        for (int i = 1; i <= m; i++) {
            ans ^= (ll)i * dp[k[i]][1];
        }
        printf("%lld\n", ans);
        return;
    }
    ll ans = 0;
    for (int i = 1; i <= m; i++) {
        if (mem[c[i]] != -1) {
            ans ^= (ll)i * mem[c[i]];
            continue;
        }
        for (int j = 1; j <= c[i]; j++) {
            ene[j] = a[j];
        }
        for (int j = c[i] + 1; j <= (1 << k[i]); j++) {
            ene[j] = 0;
        }
        int cur = getwinner(k[i], 1 << k[i]);
        if (cur > c[i]) {
            cur = 0;
        }
        // cout << cur << ' ';
        for (int j = c[i] + 1; j <= (1 << k[i]); j++) {
            ene[j] = INT_MAX;
            if (getwinner(k[i], 1 << k[i]) == j) {
                // cout << j << ' ';
                cur += j;
            }
            ene[j] = 0;
        }
        // cout << cur << '\n';
        ans ^= (ll)i * cur;
        mem[c[i]] = cur;
    }
    printf("%lld\n", ans);
}
int main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &ap[i]);
    }
    for (int i = 1; i <= m; i++) {
        scanf("%d", &c[i]);
    }
    int K = 0;
    while ((1LL << K) < n) {
        K++;
    }
    for (int i = 1; i <= K; i++) {
        for (int j = 1; j <= (1 << (K - i)); j++) {
            char c;
            scanf(" %c", &c);
            if (c == '1') {
                d[i][j] = 1;
            } else {
                d[i][j] = 0;
            }
        }
    }
    int T;
    scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}